package com.sense.kukoo.biz_messages.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.d.lib.slidelayout.SlideLayout
import com.sense.kukoo.biz_messages.R
import com.sense.kukoo.biz_messages.adapter.MessagesItemAdapter
import com.sense.kukoo.common.repo.biz.MessagesItemBean
import com.sense.kukoo.common.repo.http.bean.UserMessageCommentsBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.utils.DataTimeUtil
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class CommentsMessagesItemProvider(
    val listener: MessagesItemAdapter.MessagesItemListener,
): MultiItemProvider<MessagesItemBean>() {

    private val dp8 = ApplicationContext.dp2px(8F)

    override fun getItemType(): Int = MessagesItemBean.TYPE_COMMENTS

    override fun getItemLayoutId(): Int = R.layout.biz_messages_item_comments

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: MessagesItemBean) {
        val data: UserMessageCommentsBean = item.comment!!
        //
        val sl_item_messages_comments = viewHolder.getView(R.id.sl_item_messages_comments) as SlideLayout
        sl_item_messages_comments.setOpen(false, false)
        //
        val iv_icon_messages_item = viewHolder.getView(R.id.iv_icon_messages_item) as ImageView
        Monet.get(iv_icon_messages_item.context)
            .load<GlideConfig>(data.avatarUrl)
            .placeholder(com.sense.kukoo.common_design.R.drawable.design_icon_avatar_default)
            .circleCrop()
            .into(iv_icon_messages_item)
        //
        val iv_music_icon = viewHolder.getView(R.id.iv_music_icon) as ImageView
        Monet.get(iv_music_icon.context)
            .load<GlideConfig>(data.coverUrl)
            .connerCrop(dp8)
            .into(iv_music_icon)
        //
        val tv_name_channel = viewHolder.getView(R.id.tv_name_channel) as TextView
        tv_name_channel.text = data.channelName
        //
        val tv_comments_hint = viewHolder.getView(R.id.tv_comments_hint) as TextView
        val tv_name_reply = viewHolder.getView(R.id.tv_name_reply) as TextView
        val tv_time = viewHolder.getView(R.id.tv_time) as TextView
        if(1 == data.type){
            tv_comments_hint.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.messages_item_comment_time_hint)
            tv_name_reply.visibility = View.GONE
        } else if(2 == data.type){
            tv_comments_hint.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.messages_item_reply_time_hint)
            tv_name_reply.visibility = View.GONE
        } else if(3 == data.type){
            tv_comments_hint.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.messages_item_reply_to_hint)
            tv_name_reply.visibility = View.VISIBLE
            tv_name_reply.text = data.replyUserName ?: ""
        }
        tv_time.text = DataTimeUtil.dateTransform1(data.createdAt)
        //
        val tv_comments_text = viewHolder.getView(R.id.tv_comments_text) as TextView
        tv_comments_text.text = data.content
        //
        viewHolder.getView(R.id.click_btn_item).setOnClickListener {
            listener.onClickMusic(item)
        }
        //
        viewHolder.getView(R.id.click_btn_delete).setOnClickListener {
            listener.onDeleteItem(item)
        }
    }
}